﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class StoryNodeController
    {
        private SQLiteFactory sqlf;
        private SQLite.SQLiteConnection conn;

        public StoryNodeController()
        {
            sqlf = new SQLiteFactory();
        }

        public StoryNode getStoryNode(int storyNodeId)
        {
            conn = sqlf.getConnection();
            StoryNode s = conn.Query<StoryNode>("SELECT * FROM StoryNode WHERE id = ?", storyNodeId).Single();
            conn.Close();
            return s;
        }

        public Ontology getEvent(string option)
        {
            conn = sqlf.getConnection();
            System.Diagnostics.Debug.WriteLine("OPTION:" + option);
            Ontology s;
            try
            {
                s = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", option, "triggers").Single();
            }
            catch (InvalidOperationException e)
            {
                s = null;
            }
            conn.Close();
            return s;
        }

        public List<Ontology> getStoryCriterias(string node)
        {
            conn = sqlf.getConnection();
            List<Ontology> criteriaList = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", node, "prerequisite");
            conn.Close();
            System.Diagnostics.Debug.WriteLine("NODE NAME:" + node+" CRITERIA COUNT:" + criteriaList.Count);
            return criteriaList;
        }

        public List<Ontology> getChildNodes(string parentNode)
        {
            conn = sqlf.getConnection();
            List<Ontology> childList = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", parentNode, "parentOf");
            conn.Close();
            return childList;
        }

        public StoryNode getStoryNode(Ontology node)
        {
            conn = sqlf.getConnection();
            System.Diagnostics.Debug.WriteLine(node.concept2);
            StoryNode sn = conn.Query<StoryNode>("SELECT * FROM StoryNode WHERE name = ?", node.concept2).Single();
            conn.Close();
            return sn;
        }

        public string getPicURLOrGame(string option)
        {
            conn = sqlf.getConnection();
            Ontology so = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", option, "uses").Single();
            conn.Close();
            System.Diagnostics.Debug.WriteLine("PIC URL :" + so.concept2);
            return so.concept2;
        }

        public string getSpeaker(string node)
        {
            conn = sqlf.getConnection();
            Ontology sn = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", node, "uses").Single();
            conn.Close();
            return sn.concept2;
        }

        public List<string> getItems(string node)
        {
            conn = sqlf.getConnection();
            List<Ontology> storyOntList = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", node, "adds").ToList();
            List<string> itemNames = new List<string>();

            foreach (Ontology s in storyOntList)
            {
                itemNames.Add(s.concept2);
            }

            return itemNames;
        }

        public List<Ontology> getListDialogue(string storyNode){
            Ontology objective = getObjective(storyNode);
            conn = sqlf.getConnection();
            List<Ontology> listDialoge;
            try
            {
                listDialoge = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", objective.concept2, "useDialogue");
            }

            catch (Exception e) {
                conn.Close();
                return null;
            }
            conn.Close();
            return listDialoge;
        }

        public List<Ontology> getConstructiveCritic(string storyNode) {
            Ontology objective = getObjective(storyNode);
            conn = sqlf.getConnection();
            List<Ontology> listDialoge;
            try
            {
                listDialoge = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", objective.concept2, "hasConstructiveCriticism");
            }

            catch (Exception e)
            {
                conn.Close();
                return null;
            }
            conn.Close();
            return listDialoge;
        }

        public Ontology getObjective(string storyNode) {
            conn = sqlf.getConnection();
            Ontology objective;
            try
            {
                objective = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", storyNode, "hasObjective").Single();
            }

            catch (Exception e) {
                conn.Close();
                return null;
            }
            conn.Close();
            return objective;
        }

        public Ontology getThreshold(string dialogue) {
            conn = sqlf.getConnection();
            Ontology o = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation LIKE ?", dialogue, "%threshold%").Single();
            conn.Close();
            return o;
        }

        public int getCriteriaValue(string criteria) {
            conn = sqlf.getConnection();
            Ontology o;
            try
            {
                o = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ? AND relation = ?", criteria, "hasValue").Single();
            }
            catch (Exception e) {
                conn.Close();
                return 0;
            }
            conn.Close();
            return Convert.ToInt32(o.concept2);
        }

        

    }
}
